package com.jingdianjichi.subject.application.controller;

import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody @Validated SubjectCategoryDTO subjectCategoryDTO) {
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("添加失败", e);
            return Result.fail();
        }
    }

    @GetMapping("/queryCategoryList")
    public Result<List<SubjectCategoryDTO>> queryCategoryList(SubjectCategoryDTO subjectCategoryDTO){
        List<SubjectCategoryBO> subjectCategoryBOList
                = subjectCategoryDomainService.queryCategoryList(
                        SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO));
        List<SubjectCategoryDTO>subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.convertBOListToDTOList(subjectCategoryBOList);
        return Result.ok(subjectCategoryDTOList);
    }

    /**
     * 根据父类id查询
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryCategoryListByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryListByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(subjectCategoryDTO.getParentId() == null){
            throw new RuntimeException("父类id不能为空");
        }
        List<SubjectCategoryBO> subjectCategoryBOList
                = subjectCategoryDomainService.queryCategoryList(
                SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO));
        List<SubjectCategoryDTO>subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.convertBOListToDTOList(subjectCategoryBOList);
        return Result.ok(subjectCategoryDTOList);
    }


    @PostMapping("/deleteCategory/{id}")
    public Result<Boolean> deleteCategory(@PathVariable Long id){
        try {
            subjectCategoryDomainService.deleteCategory(id);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("删除失败", e);
            return Result.fail();
        }
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody @Validated SubjectCategoryDTO subjectCategoryDTO) {
        if(subjectCategoryDTO.getId() == null){
            throw new RuntimeException("id不能为空");
        }
        try {
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("更新失败", e);
            return Result.fail();
        }
    }

    /**
     * 根据父类id查询
     * @param subjectCategoryDTO
     * @return
     */
    @PostMapping("/queryCategoryListAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryListAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(subjectCategoryDTO.getParentId() == null){
            throw new RuntimeException("父类id不能为空");
        }
        List<SubjectCategoryBO> subjectCategoryBOList
                = subjectCategoryDomainService.queryCategoryListAndLabel(
                SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO));
        List<SubjectCategoryDTO> SubjectCategoryDTOList = new ArrayList<>();
        subjectCategoryBOList.forEach(bo->{
            SubjectCategoryDTO dto = SubjectCategoryDTOConverter.INSTANCE.convertBOToDTO(bo);
            List<SubjectLabelBO> subjectLabelBOList = bo.getSubjectLabelBOList();

            List<SubjectLabelDTO> subjectLabelDTOList = SubjectLabelDTOConverter.INSTANCE.convertBOListToDTOList(subjectLabelBOList);
            dto.setSubjectLabelDTOList(subjectLabelDTOList);
            SubjectCategoryDTOList.add(dto);
        });

        return Result.ok(SubjectCategoryDTOList);
    }

}
